<?php
	class Admincp_Model_Cart extends Zend_Db_Table_Abstract
	{
		protected $_name = 'cart';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem()
        {
            $data = array(
                'id',
                'fullname',
                'address',
                'email',
                'phone',
                'create_date',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->order('id DESC');
            return $sql;
        }
        
        public function getItem($id)
        {
            $data = array(
                'fullname',
                'address',
                'email',
                'phone',
                'create_date',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getTableForType($type)
        {
            switch($type) {
                case    1   :   $name = 'fashion'; break;
                case    2   :   $name = 'baby'; break;
                case    3   :   $name = 'furniture'; break;
            }
            return $name;
        }
        
        public function getCartInfo($cart_id)
        {
            $sql = $this->_db->select()
                            ->from('cart_info')
                            ->where('cart_id = ?', $cart_id)
                            ->order('id ASC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function getProductByCartInfo($product_id, $type)
        {
            $data = array(
                'name',
                'price',
            );
            $table = $this->getTableForType($type);
            $sql = $this->_db->select()
                            ->from($table, $data)
                            ->where('id = ?', $product_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function saveItem($data, $option = null)
        {
            if($option == 'create') {
                $this->_db->insert($this->_name, $data);
            }
            else if($option == 'update') {
                $where = $this->getAdapter()->quoteInto('id = ?', $data['id']);
                $this->_db->update($this->_name, $data, $where);
            }
        }
        
        public function deleteItem($id)
		{
		    $id = (int) $id;
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
		    $this->_db->delete($this->_name, $where);
		}
		
		public function deleteMultiItem($listId)
		{
		    foreach($listId as $value) {
		        $this->deleteItem($value);
		    }
		}
        
        public function searchItem($data)
		{
            $common = new Admincp_Model_Common();
		    $cart = array(
                'id',
                'fullname',
                'address',
                'email',
                'phone',
                'create_date',
                'status',
            );
		    $sql = $this->_db->select()
		                    ->from($this->_name, $cart);
		    if($data['fullname'] != '') {
		        $sql = $sql->where('fullname_plaintext LIKE ?', $data['fullname']);
		    }
            if($data['address'] != '') {
		        $sql = $sql->where('address_plaintext LIKE ?', $data['address']);
		    }
            if($data['email'] != '') {
		        $sql = $sql->where('email LIKE ?', $data['email']);
		    }
            if($data['phone'] != '') {
		        $sql = $sql->where('phone LIKE ?', $data['phone']);
		    }
		    if($data['start_date'] !== '') {
		        $sql = $sql->where('create_date >= ?', $common->formatDate($data['start_date']));
		    }
		    if($data['end_date'] !== '') {
		        $sql = $sql->where('create_date <= ?', $common->formatDate($data['end_date']));
		    }
		    if($data['status'] != 0) {
		        $sql = $sql->where('status = ?', $data['status']);
		    }
		    $sql = $sql->order('id DESC');
		    return $sql;
		}
    }
